﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Windows.Controls.Ribbon;

namespace ScrumMagic
{
    /// <summary>
    /// Controller that manages contextual tabs on ribbon.
    /// </summary>
    public static class RibbonTabController
    {
        private static Dictionary<CommonForm, RibbonTab> ribbonTabs = new Dictionary<CommonForm, RibbonTab>();

        private static RibbonTab currentActiveTab = null;

        private static MainWindow mainForm;

        public static MainWindow MainForm
        {
            get { return mainForm; }
            set { mainForm = value; }
        }

        /// <summary>
        /// Activates contextual tab of specified CommonForm form.
        /// </summary>
        /// <param name="commonForm">Form to activate its contextual tab.</param>
        public static void ActivateTab(CommonForm commonForm)
        {
            RibbonTab ribbonTab = commonForm.RibbonContextualTab;

            if (ribbonTab == null) return;

            if (!ribbonTabs.ContainsKey(commonForm))
            {
                ribbonTabs.Add(commonForm, ribbonTab);

                mainForm.ribbonMenu.Tabs.Add(ribbonTab);
            }
            else
            {
                ribbonTab = ribbonTabs[commonForm];
            }

            if (currentActiveTab != null)
            {
                currentActiveTab.Visibility = System.Windows.Visibility.Collapsed;
            }

            currentActiveTab = ribbonTab;
            currentActiveTab.Visibility = System.Windows.Visibility.Visible;
            mainForm.ribbonMenu.SelectedTab = currentActiveTab;
        }

        /// <summary>
        /// Closes contextual tab of specified CommonForm form.
        /// </summary>
        /// <param name="commonForm">Form to close its contextual tab.</param>
        public static void CloseTab(CommonForm commonForm)
        {
            if (ribbonTabs.ContainsKey(commonForm))
            {
                mainForm.ribbonMenu.Tabs.Remove(ribbonTabs[commonForm]);

                if (mainForm.ribbonMenu.SelectedTab == ribbonTabs[commonForm])
                {
                    mainForm.ribbonMenu.SelectedTab = mainForm.ribbonMenu.Tabs[0];
                }
            }
        }
    }
}
